package com.dut.bbs.activity;


import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;

import com.dut.bbs.R;
import com.dut.bbs.util.Dg;
import com.dut.bbs.util.NetWorkAvailable;
import com.dut.bbs.util.Settings;

public class WelcomeActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		initSettings();
		if(!NetWorkAvailable.goodNet(WelcomeActivity.this))
			Dg.showToast(WelcomeActivity.this, "你的网络不给力哦");
//		else if(!NetWorkAvailable.goodNetIp())
//			Dg.showToast(WelcomeActivity.this, "外网不能发帖回复哦，亲");
		File cache = new File(Environment.getExternalStorageDirectory(), "DUTBBS");
		if(!cache.exists()){
			cache.mkdirs();
		}
		Start();
	}

	private void initSettings() {
		// TODO Auto-generated method stub
		SharedPreferences set = getSharedPreferences("settings",Context.MODE_PRIVATE);
		Settings.showPic = set.getBoolean("isPic", false);
		Settings.showSign = set.getBoolean("isBackWord", true);
		Settings.showPhone = set.getBoolean("isPhoneType", true);
		Settings.showIP = set.getBoolean("isIP", false);
		Settings.showBar = set.getString("barStat", "1");
		Settings.auto_update = set.getBoolean("isAuto", true);
		Settings.sign = set.getString("backWords", "发送自 碧海青天Android客户端");
		Settings.signColor = set.getString("signColor", "000000");
		Settings.quickReply = set.getString("fastRe", "沙发");
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		String favStr = settings.getString("fav", "");
		if(!"".equals(favStr)){
			for(String info:favStr.split("\n")){
				Map<String,String> map = new HashMap<String,String>();
				map.put("title", "版面|"+info.split("\t")[0]+"|");
				map.put("url", info.split("\t")[1]);
				list.add(map);
			}
		}
//		for(int i=0;settings.contains("fav"+i);i++){
//			Map<String,String> map = new HashMap<String,String>();
//			String info = settings.getString("fav"+i, "\t");
//			map.put("版面|"+info.split("\t")[0]+"|", info.split("\t")[1]);
//			list.add(map);
//		}
		Settings.favList = list;
	}

	public void Start() {
		new Thread() {
			public void run() {
				try {
					Thread.sleep(2500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Intent intent = new Intent();
				intent.setClass(WelcomeActivity.this, LoginActivity.class);
				startActivity(intent);
				finish();
			}
		}.start();
	}

}
